<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - src/caffe/layers/prelu_layer.cpp</title>
  <link rel="stylesheet" type="text/css" href="../../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../../index.html">top level</a> - <a href="index.html">src/caffe/layers</a> - prelu_layer.cpp<span style="font-size: 80%;"> (source / <a href="prelu_layer.cpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">2</td>
            <td class="headerCovTableEntry">69</td>
            <td class="headerCovTableEntryLo">2.9 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:50:33</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">2</td>
            <td class="headerCovTableEntry">16</td>
            <td class="headerCovTableEntryLo">12.5 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #include &lt;algorithm&gt;</a>
<span class="lineNum">       2 </span>            : #include &lt;vector&gt;
<span class="lineNum">       3 </span>            : 
<span class="lineNum">       4 </span>            : #include &quot;caffe/filler.hpp&quot;
<span class="lineNum">       5 </span>            : 
<span class="lineNum">       6 </span>            : #include &quot;caffe/layers/neuron_layer.hpp&quot;
<span class="lineNum">       7 </span>            : #include &quot;caffe/layers/prelu_layer.hpp&quot;
<span class="lineNum">       8 </span>            : 
<span class="lineNum">       9 </span>            : namespace caffe {
<span class="lineNum">      10 </span>            : 
<span class="lineNum">      11 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      12 </span><span class="lineNoCov">          0 : void PReLULayer&lt;Dtype&gt;::LayerSetUp(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      13 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      14 </span><span class="lineNoCov">          0 :   CHECK_GE(bottom[0]-&gt;num_axes(), 2)</span>
<span class="lineNum">      15 </span>            :       &lt;&lt; &quot;Number of axes of bottom blob must be &gt;=2.&quot;;
<span class="lineNum">      16 </span><span class="lineNoCov">          0 :   PReLUParameter prelu_param = this-&gt;layer_param().prelu_param();</span>
<span class="lineNum">      17 </span><span class="lineNoCov">          0 :   int channels = bottom[0]-&gt;channels();</span>
<span class="lineNum">      18 </span><span class="lineNoCov">          0 :   channel_shared_ = prelu_param.channel_shared();</span>
<span class="lineNum">      19 </span><span class="lineNoCov">          0 :   if (this-&gt;blobs_.size() &gt; 0) {</span>
<span class="lineNum">      20 </span><span class="lineNoCov">          0 :     LOG(INFO) &lt;&lt; &quot;Skipping parameter initialization&quot;;</span>
<span class="lineNum">      21 </span>            :   } else {
<span class="lineNum">      22 </span><span class="lineNoCov">          0 :     this-&gt;blobs_.resize(1);</span>
<span class="lineNum">      23 </span><span class="lineNoCov">          0 :     if (channel_shared_) {</span>
<span class="lineNum">      24 </span><span class="lineNoCov">          0 :       this-&gt;blobs_[0].reset(new Blob&lt;Dtype&gt;(vector&lt;int&gt;(0)));</span>
<span class="lineNum">      25 </span>            :     } else {
<span class="lineNum">      26 </span><span class="lineNoCov">          0 :       this-&gt;blobs_[0].reset(new Blob&lt;Dtype&gt;(vector&lt;int&gt;(1, channels)));</span>
<span class="lineNum">      27 </span>            :     }
<span class="lineNum">      28 </span>            :     shared_ptr&lt;Filler&lt;Dtype&gt; &gt; filler;
<span class="lineNum">      29 </span><span class="lineNoCov">          0 :     if (prelu_param.has_filler()) {</span>
<span class="lineNum">      30 </span><span class="lineNoCov">          0 :       filler.reset(GetFiller&lt;Dtype&gt;(prelu_param.filler()));</span>
<span class="lineNum">      31 </span>            :     } else {
<span class="lineNum">      32 </span><span class="lineNoCov">          0 :       FillerParameter filler_param;</span>
<span class="lineNum">      33 </span><span class="lineNoCov">          0 :       filler_param.set_type(&quot;constant&quot;);</span>
<span class="lineNum">      34 </span>            :       filler_param.set_value(0.25);
<span class="lineNum">      35 </span><span class="lineNoCov">          0 :       filler.reset(GetFiller&lt;Dtype&gt;(filler_param));</span>
<span class="lineNum">      36 </span>            :     }
<span class="lineNum">      37 </span><span class="lineNoCov">          0 :     filler-&gt;Fill(this-&gt;blobs_[0].get());</span>
<span class="lineNum">      38 </span>            :   }
<span class="lineNum">      39 </span><span class="lineNoCov">          0 :   if (channel_shared_) {</span>
<span class="lineNum">      40 </span><span class="lineNoCov">          0 :     CHECK_EQ(this-&gt;blobs_[0]-&gt;count(), 1)</span>
<span class="lineNum">      41 </span>            :         &lt;&lt; &quot;Negative slope size is inconsistent with prototxt config&quot;;
<span class="lineNum">      42 </span>            :   } else {
<span class="lineNum">      43 </span><span class="lineNoCov">          0 :     CHECK_EQ(this-&gt;blobs_[0]-&gt;count(), channels)</span>
<span class="lineNum">      44 </span>            :         &lt;&lt; &quot;Negative slope size is inconsistent with prototxt config&quot;;
<span class="lineNum">      45 </span>            :   }
<span class="lineNum">      46 </span>            : 
<span class="lineNum">      47 </span>            :   // Propagate gradients to the parameters (as directed by backward pass).
<span class="lineNum">      48 </span><span class="lineNoCov">          0 :   this-&gt;param_propagate_down_.resize(this-&gt;blobs_.size(), true);</span>
<span class="lineNum">      49 </span><span class="lineNoCov">          0 :   multiplier_.Reshape(vector&lt;int&gt;(1, bottom[0]-&gt;count(1)));</span>
<span class="lineNum">      50 </span><span class="lineNoCov">          0 :   backward_buff_.Reshape(vector&lt;int&gt;(1, bottom[0]-&gt;count(1)));</span>
<span class="lineNum">      51 </span><span class="lineNoCov">          0 :   caffe_set(multiplier_.count(), Dtype(1), multiplier_.mutable_cpu_data());</span>
<span class="lineNum">      52 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      53 </span>            : 
<span class="lineNum">      54 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      55 </span><span class="lineNoCov">          0 : void PReLULayer&lt;Dtype&gt;::Reshape(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      56 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      57 </span><span class="lineNoCov">          0 :   CHECK_GE(bottom[0]-&gt;num_axes(), 2)</span>
<span class="lineNum">      58 </span>            :       &lt;&lt; &quot;Number of axes of bottom blob must be &gt;=2.&quot;;
<span class="lineNum">      59 </span><span class="lineNoCov">          0 :   top[0]-&gt;ReshapeLike(*bottom[0]);</span>
<span class="lineNum">      60 </span><span class="lineNoCov">          0 :   if (bottom[0] == top[0]) {</span>
<span class="lineNum">      61 </span>            :     // For in-place computation
<span class="lineNum">      62 </span><span class="lineNoCov">          0 :     bottom_memory_.ReshapeLike(*bottom[0]);</span>
<span class="lineNum">      63 </span>            :   }
<span class="lineNum">      64 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      65 </span>            : 
<span class="lineNum">      66 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      67 </span><span class="lineNoCov">          0 : void PReLULayer&lt;Dtype&gt;::Forward_cpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      68 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      69 </span><span class="lineNoCov">          0 :   const Dtype* bottom_data = bottom[0]-&gt;cpu_data();</span>
<span class="lineNum">      70 </span><span class="lineNoCov">          0 :   Dtype* top_data = top[0]-&gt;mutable_cpu_data();</span>
<span class="lineNum">      71 </span><span class="lineNoCov">          0 :   const int count = bottom[0]-&gt;count();</span>
<span class="lineNum">      72 </span>            :   const int dim = bottom[0]-&gt;count(2);
<span class="lineNum">      73 </span><span class="lineNoCov">          0 :   const int channels = bottom[0]-&gt;channels();</span>
<span class="lineNum">      74 </span><span class="lineNoCov">          0 :   const Dtype* slope_data = this-&gt;blobs_[0]-&gt;cpu_data();</span>
<span class="lineNum">      75 </span>            : 
<span class="lineNum">      76 </span>            :   // For in-place computation
<span class="lineNum">      77 </span><span class="lineNoCov">          0 :   if (bottom[0] == top[0]) {</span>
<span class="lineNum">      78 </span><span class="lineNoCov">          0 :     caffe_copy(count, bottom_data, bottom_memory_.mutable_cpu_data());</span>
<span class="lineNum">      79 </span>            :   }
<span class="lineNum">      80 </span>            : 
<span class="lineNum">      81 </span>            :   // if channel_shared, channel index in the following computation becomes
<span class="lineNum">      82 </span>            :   // always zero.
<span class="lineNum">      83 </span><span class="lineNoCov">          0 :   const int div_factor = channel_shared_ ? channels : 1;</span>
<span class="lineNum">      84 </span><span class="lineNoCov">          0 :   for (int i = 0; i &lt; count; ++i) {</span>
<span class="lineNum">      85 </span><span class="lineNoCov">          0 :     int c = (i / dim) % channels / div_factor;</span>
<span class="lineNum">      86 </span><span class="lineNoCov">          0 :     top_data[i] = std::max(bottom_data[i], Dtype(0))</span>
<span class="lineNum">      87 </span><span class="lineNoCov">          0 :         + slope_data[c] * std::min(bottom_data[i], Dtype(0));</span>
<span class="lineNum">      88 </span>            :   }
<span class="lineNum">      89 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      90 </span>            : 
<span class="lineNum">      91 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      92 </span><span class="lineNoCov">          0 : void PReLULayer&lt;Dtype&gt;::Backward_cpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top,</span>
<span class="lineNum">      93 </span>            :     const vector&lt;bool&gt;&amp; propagate_down,
<span class="lineNum">      94 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom) {
<span class="lineNum">      95 </span><span class="lineNoCov">          0 :   const Dtype* bottom_data = bottom[0]-&gt;cpu_data();</span>
<span class="lineNum">      96 </span><span class="lineNoCov">          0 :   const Dtype* slope_data = this-&gt;blobs_[0]-&gt;cpu_data();</span>
<span class="lineNum">      97 </span><span class="lineNoCov">          0 :   const Dtype* top_diff = top[0]-&gt;cpu_diff();</span>
<span class="lineNum">      98 </span><span class="lineNoCov">          0 :   const int count = bottom[0]-&gt;count();</span>
<span class="lineNum">      99 </span>            :   const int dim = bottom[0]-&gt;count(2);
<span class="lineNum">     100 </span><span class="lineNoCov">          0 :   const int channels = bottom[0]-&gt;channels();</span>
<span class="lineNum">     101 </span>            : 
<span class="lineNum">     102 </span>            :   // For in-place computation
<span class="lineNum">     103 </span><span class="lineNoCov">          0 :   if (top[0] == bottom[0]) {</span>
<span class="lineNum">     104 </span><span class="lineNoCov">          0 :     bottom_data = bottom_memory_.cpu_data();</span>
<span class="lineNum">     105 </span>            :   }
<span class="lineNum">     106 </span>            : 
<span class="lineNum">     107 </span>            :   // if channel_shared, channel index in the following computation becomes
<span class="lineNum">     108 </span>            :   // always zero.
<span class="lineNum">     109 </span><span class="lineNoCov">          0 :   const int div_factor = channel_shared_ ? channels : 1;</span>
<span class="lineNum">     110 </span>            : 
<span class="lineNum">     111 </span>            :   // Propagte to param
<span class="lineNum">     112 </span>            :   // Since to write bottom diff will affect top diff if top and bottom blobs
<span class="lineNum">     113 </span>            :   // are identical (in-place computaion), we first compute param backward to
<span class="lineNum">     114 </span>            :   // keep top_diff unchanged.
<span class="lineNum">     115 </span><span class="lineNoCov">          0 :   if (this-&gt;param_propagate_down_[0]) {</span>
<span class="lineNum">     116 </span><span class="lineNoCov">          0 :     Dtype* slope_diff = this-&gt;blobs_[0]-&gt;mutable_cpu_diff();</span>
<span class="lineNum">     117 </span><span class="lineNoCov">          0 :     for (int i = 0; i &lt; count; ++i) {</span>
<span class="lineNum">     118 </span><span class="lineNoCov">          0 :       int c = (i / dim) % channels / div_factor;</span>
<span class="lineNum">     119 </span><span class="lineNoCov">          0 :       slope_diff[c] += top_diff[i] * bottom_data[i] * (bottom_data[i] &lt;= 0);</span>
<span class="lineNum">     120 </span>            :     }
<span class="lineNum">     121 </span>            :   }
<span class="lineNum">     122 </span>            :   // Propagate to bottom
<span class="lineNum">     123 </span><span class="lineNoCov">          0 :   if (propagate_down[0]) {</span>
<span class="lineNum">     124 </span><span class="lineNoCov">          0 :     Dtype* bottom_diff = bottom[0]-&gt;mutable_cpu_diff();</span>
<span class="lineNum">     125 </span><span class="lineNoCov">          0 :     for (int i = 0; i &lt; count; ++i) {</span>
<span class="lineNum">     126 </span><span class="lineNoCov">          0 :       int c = (i / dim) % channels / div_factor;</span>
<span class="lineNum">     127 </span><span class="lineNoCov">          0 :       bottom_diff[i] = top_diff[i] * ((bottom_data[i] &gt; 0)</span>
<span class="lineNum">     128 </span><span class="lineNoCov">          0 :           + slope_data[c] * (bottom_data[i] &lt;= 0));</span>
<span class="lineNum">     129 </span>            :     }
<span class="lineNum">     130 </span>            :   }
<span class="lineNum">     131 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">     132 </span>            : 
<a name="133"><span class="lineNum">     133 </span>            : </a>
<span class="lineNum">     134 </span>            : #ifdef CPU_ONLY
<span class="lineNum">     135 </span><span class="lineNoCov">          0 : STUB_GPU(PReLULayer);</span>
<span class="lineNum">     136 </span>            : #endif
<a name="137"><span class="lineNum">     137 </span>            : </a>
<span class="lineNum">     138 </span>            : INSTANTIATE_CLASS(PReLULayer);
<a name="139"><span class="lineNum">     139 </span><span class="lineCov">          3 : REGISTER_LAYER_CLASS(PReLU);</span></a>
<span class="lineNum">     140 </span>            : 
<span class="lineNum">     141 </span><span class="lineCov">          3 : }  // namespace caffe</span>
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
